﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using FluentFTP.Configuration;
using FluentFTP.Dialogs;
using FluentFTP.Model;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

namespace FluentFTP.ViewModel
{
    internal class ConnectionViewModel : NotificationObject
    {
        private Action<Tuple<string, string, string>> connectAction;
        private const string ftpPrefix = "ftp://";

        #region Properties

        private FtpServerInfo server;

        public FtpServerInfo Server
        {
            get { return server; }
            set
            {
                server = value;

                server.PropertyChanged += (_, args) =>
                    {
                        ConnectCommand.RaiseCanExecuteChanged();
                    };

                ConnectCommand.RaiseCanExecuteChanged();
                RaisePropertyChanged(() => Server);
            }
        }

        private ObservableCollection<FtpServerInfo> savedServers;

        public ObservableCollection<FtpServerInfo> SavedServers
        {
            get { return savedServers; }
            set { savedServers = value; }
        }

        private FtpServerInfo selectedSavedServer;

        public FtpServerInfo SelectedSavedServer
        {
            get { return selectedSavedServer; }
            set
            {
                selectedSavedServer = value;
                Server = SelectedSavedServer;
            }
        }

        private bool saveServer = true;

        public bool SaveServer
        {
            get { return saveServer; }
            set { saveServer = value; }
        }

        #endregion Properties

        public DelegateCommand ConnectCommand { get; private set; }

        public ConnectionViewModel(Action<Tuple<string, string, string>> connectAction)
        {
            ConnectCommand = new DelegateCommand(() =>
                {
                    if (SaveServer)
                    {
                        if (!Settings.Servers.Any(x => x.Id == Server.Id))
                        {
                            var nameDialog = new TextBoxDialog("Server Name", name => save(name, Server.Adress, Server.UserName, Server.Password));
                            nameDialog.ShowDialog();
                        }
                        else
                        {
                            Settings.Servers.Remove(Settings.Servers.Find(x => x.Id == Server.Id));
                            Settings.Servers.Add(Server);
                        }
                    }

                    connectAction(new Tuple<string, string, string>(Server.Adress, Server.UserName, Server.Password));
                },
                () => Server != null && !string.IsNullOrEmpty(Server.Adress) && !string.IsNullOrEmpty(Server.UserName));

            this.connectAction = connectAction;
            SavedServers = new ObservableCollection<FtpServerInfo>(Settings.Servers);

            Server = new FtpServerInfo { Password = Settings.UserSettings.DefaultPassword, UserName = Settings.UserSettings.DefaultUserName, Adress = ftpPrefix, Id = Guid.NewGuid(), Name = "< New >" };
            SavedServers.Insert(0, Server);
            SelectedSavedServer = Server;

            ConnectCommand.RaiseCanExecuteChanged();
        }

        private void save(string name, string address, string userName, string password)
        {
            Settings.Servers.Add(new FtpServerInfo { Name = name, Adress = address, UserName = userName, Password = password });
        }
    }
}